# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>

_realname=plplot
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.11.0
pkgrel=2
arch=('any')
pkgdesc="Scientific plotting software (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-lua"
         "${MINGW_PACKAGE_PREFIX}-python2"
         "${MINGW_PACKAGE_PREFIX}-python2-numpy"
         "${MINGW_PACKAGE_PREFIX}-qhull"
         "${MINGW_PACKAGE_PREFIX}-swig"
         "${MINGW_PACKAGE_PREFIX}-tk"
         "${MINGW_PACKAGE_PREFIX}-wxWidgets"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran"
             "${MINGW_PACKAGE_PREFIX}-gcc-ada")
options=('strip' 'staticlibs')
license=('LGPL')
url="http://plplot.sourceforge.net"
source=(https://downloads.sourceforge.net/sourceforge/plplot/${_realname}-${pkgver}.tar.gz
        find-gd.patch)
sha256sums=('bfa8434e6e1e7139a5651203ec1256c8581e2fac3122f907f7d8d25ed3bd5f7e'
            'd5fbcb9369824c0f3da24bc63fcf4d764bbf70a2c727f7fec6ad3bf42edaa92f')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/find-gd.patch
}

build() {
  # cmake's FindSWIG doesn't work properly on MSYS,
  # so we provide SWIG here
  SWIG_DIR_U=`${MINGW_PREFIX}/bin/swig -swiglib`
  SWIG_DIR=`cygpath -m ${SWIG_DIR_U}`

  # cmake may be confused if there is a Windows installation
  # of the official Python MSI. Please do the Python-enabled
  # build only, if no official Python is installed.
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_Fortran_COMPILER=${MINGW_PREFIX}/bin/gfortran.exe \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=OFF \
    -DSWIG_EXECUTABLE="${SWIG_EXECUTABLE}" \
    -DSWIG_DIR="${SWIG_DIR}" \
    -DQHULL_INCLUDE_DIR=${MINGW_PREFIX}/include \
    -DGD_INCLUDE_DIR=${MINGW_PREFIX}/include \
    -DENABLE_lua=ON \
    -DENABLE_python=ON \
    ../${_realname}-${pkgver}

  make V=1
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  mkdir -p ${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname
  cp -pf ${srcdir}/${_realname}-${pkgver}/Copyright \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}
  cp -pf ${srcdir}/${_realname}-${pkgver}/COPYING* \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}
}
